package com.liz.lizrpccore.beans.proxys.netty;

import com.liz.lizrpcapi.commons.Node;
import com.liz.lizrpccore.beans.proxys.base.LizAbstractProxy;
import com.liz.lizrpccore.core.LizRpcConfig;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author liangGongWei
 * @description
 * @create 2021-09-22 13:55
 **/

public class LizNettyProxy extends NettyBase implements LizAbstractProxy {
    @Override
    public <T> Object getResult(Class<T> interfaceType, Method method, Object[] args) {
        Map<String, Node> nodeMap = LizRpcConfig.classNodeMap.get(interfaceType.getName());
        Node node = this.getNodeBalanced(nodeMap);
        return super.getResultFromRemoteNode(interfaceType,method,args,node);
    }



    /**
     * 这里假装实现了负载均衡的功能，现在就直接返回第一个
     */
    protected Node getNodeBalanced(Map<String, Node> nodeMap) {
        Node node = null;
        for (Map.Entry<String, Node> entry : nodeMap.entrySet()) {
            node = entry.getValue();
            break;
        }
        return node;
    }
}
